package com.lanlian.cayman.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.EventFundDetailsFilterData;
import com.lanlian.cayman.fragment.FundDetailsFeeFragment;
import com.lanlian.cayman.fragment.FundDetailsPnlFragment;
import com.lanlian.cayman.fragment.FundDetailsRechargeFragment;
import com.lanlian.cayman.fragment.FundDetailsWithdrawFragment;
import com.lanlian.cayman.view.DetailsFilterWindow;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.utillib.DateUtils;
import com.lanlian.utillib.DisplayUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 16:51
 *     desc   : 资金明细
 *     version: 1.0
 * </pre>
 */
public class  TradeDetailsFundActivity extends BaseActivity implements View.OnClickListener {

    private String[] mTitleList = new String[]{"充值", "提现", "手续费", "盈亏"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private MagicIndicator mMagicIndicator;
    private ViewPager mViewPager;
    private DetailsFilterWindow mPopupWindow1;
    private DetailsFilterWindow mPopupWindow2;
    private DetailsFilterWindow mPopupWindow3;
    private DetailsFilterWindow mPopupWindow0;

    public static int FUND_DETAILS_TYPE_RECHARGE = 1;
    public static int FUND_DETAILS_TYPE_WITHDRAW = 2;
    public static int FUND_DETAILS_TYPE_FEE = 3;
    public static int FUND_DETAILS_TYPE_PNL = 4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_details_fund, true);
        initView();
        setListeners();
        initFragments();
        initNavigator();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("资金流水");
        setRightImage(R.mipmap.detailed_icon_screen);
        mMagicIndicator = findViewById(R.id.magic_indicator);
        mViewPager = findViewById(R.id.view_pager);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgAction.setOnClickListener(this);
    }

    private void initFragments() {
        mFragments.add(new FundDetailsRechargeFragment());
        mFragments.add(new FundDetailsWithdrawFragment());
        mFragments.add(new FundDetailsFeeFragment());
        mFragments.add(new FundDetailsPnlFragment());
    }

    private void initNavigator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.color_K4));
                colorTransitionPagerTitleView.setTextSize(14);
                colorTransitionPagerTitleView.setSelectedColor(Color.WHITE);
                colorTransitionPagerTitleView.setText(mTitleList[index]);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                float borderWidth = UIUtil.dip2px(context, 1);
                indicator.setLineHeight(DisplayUtil.dip2px(context, 36) - 2 * borderWidth);
                indicator.setYOffset(borderWidth);
                indicator.setRoundRadius(DisplayUtil.dip2px(context, 2));
                indicator.setColors(getResources().getColor(R.color.color_K4));
                return indicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer();
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.divider2));
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_action:
                switch (mViewPager.getCurrentItem()) {
                    case 0:
                        if (mPopupWindow0 == null) {
                            mPopupWindow0 = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                                @Override
                                public void onFilter(long beginDate, long endDate) {
                                    String mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                                    String mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_FUND_DETAILS_REFRESH,
                                            new EventFundDetailsFilterData(mBeginDate, mEndDate)));
                                }
                            });
                        }
                        mPopupWindow0.showAsDropDown(v, 0, 0);
                        break;
                    case 1:
                        if (mPopupWindow1 == null) {
                            mPopupWindow1 = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                                @Override
                                public void onFilter(long beginDate, long endDate) {
                                    String mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                                    String mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_FUND_DETAILS_REFRESH,
                                            new EventFundDetailsFilterData(mBeginDate, mEndDate)));
                                }
                            });
                        }
                        mPopupWindow1.showAsDropDown(v, 0, 0);
                        break;
                    case 2:
                        if (mPopupWindow2 == null) {
                            mPopupWindow2 = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                                @Override
                                public void onFilter(long beginDate, long endDate) {
                                    String mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                                    String mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_FUND_DETAILS_REFRESH,
                                            new EventFundDetailsFilterData(mBeginDate, mEndDate)));
                                }
                            });
                        }
                        mPopupWindow2.showAsDropDown(v, 0, 0);
                        break;
                    case 3:
                        if (mPopupWindow3 == null) {
                            mPopupWindow3 = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                                @Override
                                public void onFilter(long beginDate, long endDate) {
                                    String mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                                    String mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_FUND_DETAILS_REFRESH,
                                            new EventFundDetailsFilterData(mBeginDate, mEndDate)));
                                }
                            });
                        }
                        mPopupWindow3.showAsDropDown(v, 0, 0);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

}
